package im.beans;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 新加好友请求表
 * example:{"key":"new_friend_request","data":{'userId':'1','userName':'lk','message':'I am zs'}}
 * @author 凯
 *
 */
@Entity
@Table(name="t_IM_AddNewFriends")
public class AddNewFriends {
	
	
	public static final int USER_LIMIT = 4;

	private long id;
	
	// 好友请求者id
	private long asker_id;
	
	// 请求者名字
	private String asker_name;
	
	// 被请求者id
	private long be_asked_id;
	
	// 被请求者名字
	private String be_asked_name;
	
	
	// 好友请求者
	private User askerUser;
	
	// 被请求者
	private User beaskerUser;
	
	// 好友请求状态
	/**
	 * 1：发出请求，等待处理。
	 * 2：用户拒绝。
	 * 3：用户接受。
	 * 4：加入禁止
	 */
	private int state;
	
	// 附加消息
	private String message;

	@Id
	@GeneratedValue
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getAsker_id() {
		return asker_id;
	}

	public void setAsker_id(long asker_id) {
		this.asker_id = asker_id;
	}

	public long getBe_asked_id() {
		return be_asked_id;
	}

	public void setBe_asked_id(long be_asked_id) {
		this.be_asked_id = be_asked_id;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public String getAsker_name() {
		return asker_name;
	}

	public void setAsker_name(String asker_name) {
		this.asker_name = asker_name;
	}

	public String getBe_asked_name() {
		return be_asked_name;
	}

	public void setBe_asked_name(String be_asked_name) {
		this.be_asked_name = be_asked_name;
	}

	
	
	@ManyToOne
	@JoinColumn(name="asker_id",insertable=false,updatable=false)
	public User getAskerUser() {
		return askerUser;
	}

	public void setAskerUser(User askerUser) {
		this.askerUser = askerUser;
	}

	@ManyToOne
	@JoinColumn(name="be_asked_id",insertable=false,updatable=false)
	public User getBeaskerUser() {
		return beaskerUser;
	}

	public void setBeaskerUser(User beaskerUser) {
		this.beaskerUser = beaskerUser;
	}

	/**
	 * 转为json
	 */
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("{");
		sb.append("\"id\":\"").append(id).append("\",");
		sb.append("\"asker_id\":\"").append(asker_id).append("\",");
		sb.append("\"be_asked_id\":\"").append(be_asked_id).append("\",");
		sb.append("\"state\":\"").append(state).append("\",");
		sb.append("\"message\":\"").append(message).append("\"");
		sb.append("}");
		return sb.toString();
		
	}
	
	
	/**
	 * 集合转json
	 */
	public static String list2Json(List<AddNewFriends> adnfs)
	{
		StringBuffer sb = new StringBuffer();
		sb.append("{\"GetFriendRequest\":");
		sb.append("[");
		boolean hasResult = false;
		for(AddNewFriends af: adnfs)
		{
			hasResult = true;
			sb.append(af.toString()).append(",");
		}
		
		if (hasResult) {
			sb.deleteCharAt(sb.length()-1);
		}
		sb.append("]}");
		return sb.toString();
		
	}
	
}
